import request from '@/utils/request'
import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
import CustomForm from './Form/index.vue'
import { mapGetters } from 'vuex'
import { previewDataInterface } from '@/api/systemData/dataInterface'
import { status } from 'nprogress'
export default {
  name: 'campus',
  components: { CustomForm },
  data() {
    return {
      showAll: false,
      query: {
        brand: 'bell',
        enCode: undefined,
        area: undefined,
        type: undefined,
        stateMark: undefined,
        openDate: undefined,
        businessName: undefined,
        contact: undefined,
        endDate: undefined,
        userId: undefined,
        status: ['expires','normal','suspended']
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      },
      formVisible: false,
      exportBoxVisible: false,
      statusVisible: false,
      columnList: [{ prop: 'brand', label: '校区品牌' }, { prop: 'code', label: '校区编码' }, { prop: 'fullName', label: '校区全称' }, { prop: 'investors', label: '投资人' }, { prop: 'shortName', label: '校区简称' }, { prop: 'telephone', label: '固定电话' }, { prop: 'email', label: '校区邮箱' }, { prop: 'siteArea', label: '场地面积' }, { prop: 'manager', label: '负责人' }, { prop: 'managerMobile', label: '电话' }, { prop: 'wechat', label: '微信' }, { prop: 'provinceId', label: '省份' }, { prop: 'cityId', label: '城市' }, { prop: 'type', label: '校区类型' }, { prop: 'status', label: '校区状态' }, { prop: 'openDate', label: '开业日期' }, { prop: 'startDate', label: '服务开始日期' }, { prop: 'endDate', label: '服务截止日期' }, { prop: 'insurance', label: '保险备注' }, { prop: 'insuranceNo', label: '保险单号' }, { prop: 'insuranceDate', label: '保险到期日' }, { prop: 'businessName', label: '营业执照主体' }, { prop: 'businessNo', label: '营业执照号' }],
      typeOptions: [],
      statusOptions: [],
      brandOptions: [],
      levelOptions: []
    }
  },
  computed: {
    ...mapGetters(['allocationhandleSelection'])
  },
  created() {
    this.initData()
    this.gettypeOptions()
    this.getstateMarkOptions()
    this.getBrandOptions()
    this.getLevelOptions()
  },
  methods: {
    handleSelectionChange(rows) {
      this.$store.dispatch('allocation/handleSelection', {
        rows: rows,
        list: this.list
      })
    },
    addBatchOrUpdateHandle(type) {
      if (this.allocationhandleSelection.length < 1) {
        this.$message.error('请先选择校区')
      } else {
        this.formVisible = true
        this.$nextTick(() => {
          this.$refs.batchForm.init(type)
        })
      }
    },
    getBrandOptions() {
      getDictionaryDataSelector('Brand').then(res => {
        this.brandOptions = res.data.list
      })
    },
    getLevelOptions() {
      getDictionaryDataSelector('CampusLevel').then(res => {
        this.levelOptions = res.data.list
      })
    },
    gettypeOptions() {
      getDictionaryDataSelector('CampusType').then(res => {
        this.typeOptions = res.data.list
      })
    },
    getstateMarkOptions() {
      getDictionaryDataSelector('CampusStatus').then(res => {
        this.statusOptions = res.data.list
      })
    },
    initData() {
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `api/campus`,
        method: 'GET',
        data: query
      }).then(res => {
        this.list = res.data.list
        this.total = res.data.pagination.total
        this.listLoading = false

        let rows = []
        if (this.allocationhandleSelection.length > 0) {
          this.list.map(item => {
            if (this.allocationhandleSelection.some(handle => handle.id === item.id)) {
              rows.push(item)
            }
          })
          this.$nextTick(() => {
            if (rows) {
              rows.forEach(row => {
                this.$refs['checked-table'].$refs['CustomTable'].toggleRowSelection(row, true)
              })
            }
          })
        }
      })
    },
    onSortChange(column) {
      if (column.order == 'ascending') {
        this.listQuery.sidx = column.prop
        this.listQuery.sort = 'asc'
      } else if (column.order == 'descending') {
        this.listQuery.sidx = column.prop
        this.listQuery.sort = 'desc'
      } else {
        this.listQuery.sidx = 'createTime'
        this.listQuery.sort = 'desc'
      }
      this.initData()
    },
    changeStatusHandle(campusId, status) {
      this.statusVisible = true
      this.$nextTick(() => {
        this.$refs.StatusBox.init(campusId, status)
      })
    },
    changeTransferHandle(campusId, level) {
      this.listLoading = true
      this.$nextTick(() => {
        request({
          url: `api/campus/transfer/${level}`,
          method: 'POST',
          data: { ids: campusId }
        }).then(res => {
          this.$message({
            type: 'success',
            message: res.msg,
            onClose: () => {
              this.listLoading = false
            }
          })
        })
      })
    },

    handleDel(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(() => {
          request({
            url: `api/campus/${id}`,
            method: 'DELETE'
          }).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData()
              }
            })
          })
        })
        .catch(() => {})
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs.CustomForm.init(id, isDetail)
      })
    },
    exportData() {
      this.exportBoxVisible = true
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList)
      })
    },
    download(data) {
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query1 = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query1[key] = _query[key].join()
        } else {
          query1[key] = _query[key]
        }
      }
      let query = { ...data, ...query1 }
      request({
        url: `api/campus/Actions/Export`,
        method: 'GET',
        data: query
      }).then(res => {
        if (!res.data.url) return
        window.open(this.define.comUrl + res.data.url)
        this.$refs.ExportBox.visible = false
        this.exportBoxVisible = false
      })
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      }
      this.initData()
    },
    refresh(isrRefresh) {
      this.formVisible = false
      if (isrRefresh) this.reset()
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      }
      this.initData()
    }
  }
}
